//
//  DZWebViewController.m
//  Meidaojia
//
//  Created by Darren on 15/5/29.
//  Copyright (c) 2015年 Darren Zheng. All rights reserved.
//

#import "DZWebViewController.h"
#import "ActionView.h"
#import "ZCNewTabBarController.h"
#import "YYaoViewController.h"
#import "ZCLoginVC.h"
@interface DZWebViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webViewMain;
@property (nonatomic,strong)NSDictionary *dataDict;
@property (nonatomic,strong)UIButton *rightButton;
@end

@implementation DZWebViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initnavegation];
    self.dataDict = [NSDictionary dictionary];
    
    self.navigationItem.title = self.title;
    
    [self.view addSubview:self.webViewMain];
    [self.webViewMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];

    [self loadData];

}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
    
    if (self.urlID) {
        UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame = CGRectMake(0, 0, 24, 22);
        [rightButton setImage:[UIImage imageNamed:@"main_share"] forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(shareButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem * barButtonItem_rightNavBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        
        rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame = CGRectMake(0, 0, 24, 22);
        [rightButton addTarget:self action:@selector(collectionButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem * Item_rightNavBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        
        self.navigationItem.rightBarButtonItems = @[Item_rightNavBar,barButtonItem_rightNavBar];
    }
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)loadData{
    
    [self.webViewMain loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.strURL]]];
}

#pragma mark - UIWebViewDelegate Methods
//
//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//    NSLog(@"%@", request.URL);
//    NSString *requestString = [NSString stringWithFormat:@"%@", request.URL];
//    NSString *string = [requestString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSString *protocol = @"app-call://";
//    if ([string hasPrefix:protocol]) {
//        NSString *urlType = [[string componentsSeparatedByString:@"|"] lastObject];
//        if ([urlType isEqualToString:@"1"]) {//1食品安全小卫士
//            ZCNewTabBarController *newvc = [[ZCNewTabBarController alloc]init];
//            newvc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:newvc animated:YES];
//        }else if ([urlType isEqualToString:@"2"]){//2摇一摇抽奖
//            YYaoViewController *vc = [[YYaoViewController alloc]init];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
//        }else{//3你投票我检测
//            NSLog(@"你投票我检测");
//            if (UserModel.password.length>1) {
//                [self usedWebViewController:@"3" andTitle:@"你投票我检测"];
//            }else{
//                ZCLoginVC *login = [[ZCLoginVC alloc]init];
//                login.hidesBottomBarWhenPushed = YES;
//                [self.navigationController pushViewController:login animated:YES];
//            }
//        }
//
////        NSArray *arr = [string componentsSeparatedByString:@"//"];
////        if (arr.count > 1) {
////            NSDictionary *dic = dictionaryWithJsonString(arr[1]);
////            if ([dic[@"action"] isEqualToString:@"order"]) {
////                if (UserDefaults.mid.length < 1 && UserDefaults.password.length < 1) {
////                    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[MLoginVC new]] animated:YES completion:nil];
////                    return NO;
////                }
////                MOrderVC *vc = [[MOrderVC alloc] init];
////                vc.activityid = [NSString stringWithFormat:@"%@", dic[@"activityid"]];
////                vc.pid = [NSString stringWithFormat:@"%@", dic[@"pid"]];
////                vc.fid = [NSString stringWithFormat:@"%@", dic[@"fid"]];
////                vc.service_type = [NSString stringWithFormat:@"%@", dic[@"service_type"]];
////                vc.address = [NSString stringWithFormat:@"%@", dic[@"service_address"]];
////                [self.navigationController pushViewController:vc animated:YES];
////                
////                return NO;
////            }
////        }
//    }
//    return YES;
//}

#pragma mark 调用webView
-(void)usedWebViewController:(NSString *)url andTitle:(NSString *)title{
    
    if ([url isEqualToString:@"3"]) {
        url = [NSString stringWithFormat:@"%@/%@%@",[NetworkManager strURLBasic],@"index.php/M/Acsuzhou/like_product_list_app.html?mid=",UserModel.mid];
        NSLog(@"url === %@",url);
    }
    DZWebViewController *vc = [[DZWebViewController alloc] init];
    vc.strURL = url;
    vc.title = title;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)webViewDidStartLoad:(UIWebView *)webView{
    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    _webViewMain.hidden = NO;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    self.bannerView.hidden = NO;
}

- (UIWebView *)webViewMain
{
    if (!_webViewMain)
    {
        _webViewMain = [UIWebView new];
        _webViewMain.delegate = self;
        _webViewMain.backgroundColor = [UIColor whiteColor];
        _webViewMain.hidden = YES;
    }
    return _webViewMain;
}

#pragma mark 分享点击
-(void)shareButtonPressed:(id *)button{
    ActionView *action = [[ActionView alloc]initWithFrame:CGRectZero];
    action.pid = self.urlID;
    action.isweb = YES;
    [action showInView:self.view];
}

#pragma  mark 收藏
-(void)collectionButtonPressed{
    
}
@end
